package com.xiangxiao.rpan.storage.controller;

import com.xiangxiao.rpan.storage.chain.FileChain;
import com.xiangxiao.rpan.storage.chain.FileContext;
import com.xiangxiao.rpan.storage.constant.ResponseCode;
import com.xiangxiao.rpan.storage.constant.ResponseMessage;
import com.xiangxiao.rpan.storage.dto.FileQueryDto;
import com.xiangxiao.rpan.storage.dto.JsonObject;
import com.xiangxiao.rpan.storage.entity.FileItemResult;
import com.xiangxiao.rpan.storage.exception.StorageExcetion;
import com.xiangxiao.rpan.storage.factory.StorageSourceContext;
import com.xiangxiao.rpan.storage.service.AbstractBaseFileService;
import com.xiangxiao.rpan.storage.service.StorageSourceService;
import com.xiangxiao.rpan.storage.vo.FileInfoResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/12/26 14:40
 */
@RestController
@RequestMapping("/api/storage")
public class FileController {
  @Resource
  private FileChain fileChain;

  @Resource
  private StorageSourceContext storageSourceContext;

  @Resource
  private StorageSourceService storageSourceService;

  @PostMapping("/files")
  public JsonObject listFiles(@RequestBody FileQueryDto fileQueryDto) {
    try {
      String storageKey = fileQueryDto.getStorageKey();
      Integer storageId = storageSourceService.findIdByKey(storageKey);
      if (storageId == null) {
        throw new StorageExcetion("通过存储源 key 未找到存储源, key: " + storageKey);
      };

      // 处理请求参数默认值
      fileQueryDto.handleDefaultValue();
      AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageId(storageId);
      List<FileItemResult> fileItemList = fileService.fileList(fileQueryDto.getPath());
      // 执行责任链
      FileContext fileContext = FileContext.builder()
          .storageId(storageId)
          .fileListRequest(fileQueryDto)
          .fileItemList(fileItemList).build();
      fileChain.execute(fileContext);

      return new JsonObject(ResponseMessage.QUERY_FILE_LIST_SUCCESS, ResponseCode.OK,
          new FileInfoResultVo(fileContext.getFileItemList(), fileContext.getPasswordPattern()));
    } catch (Exception e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_FILE_LIST_FAILED, ResponseCode.QUERY_FILE_LIST_FAILED);
    }

  }

  @PostMapping("/file/item")
  public JsonObject getFileItem(@RequestBody FileQueryDto fileQueryDto) {
    String storageKey = fileQueryDto.getStorageKey();
    Integer storageId = storageSourceService.findIdByKey(storageKey);
    if (storageId == null) {
      throw new StorageExcetion("通过存储源 key 未找到存储源, key: " + storageKey);
    }

    // 处理请求参数默认值
    fileQueryDto.handleDefaultValue();

    // 获取文件列表
    AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageId(storageId);

    FileItemResult fileItemResult;
    try {
      fileItemResult = fileService.getFileItem(fileQueryDto.getPath());
    } catch (Exception e) {
      return new JsonObject("获取文件信息失败: " + e.getMessage());
    }

    return new JsonObject(ResponseMessage.QUERY_FILE_ITEM_SUCCESS, ResponseCode.OK, fileItemResult);
  }
}
